// vi:si:et:sw=4:sts=4:ts=4
package cat.hex.trainoid;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TrackList extends ListActivity
{
	private DbAdapter mDbHelper;

	//private trainoid mtrainoid;
    protected GPSReceiver service;


	static final private String tag = "TrackList"; // for Log

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.track_list);

		service = GPSReceiver.getInstance();

		mDbHelper = new DbAdapter(this);
		mDbHelper.openReadonly();

		//mtrainoid = trainoid.getInstance();

		fillData();
		//registerForContextMenu(getListView());
	}

	@Override
	protected void onResume()
	{
		/*
		 * onResume is is always called after onStart, even if the app hasn't been
		 * paused
		 *
		 * add location listener and request updates every 1000ms or 10m
		 */
		//lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		/* Suspending GPS when the Activity is Paused */
		//lm.removeUpdates(this);
		super.onPause();
	}

	/*
	   private void printLocation(Location location)
	   {
	// display some of the data in the TextView
	StringBuilder sb = new StringBuilder(512);

	sb.append("# of received positions: ");
	sb.append(noOfFixes);
	sb.append('\n');

	sb.append("Latitude: ");
	sb.append(location.getLatitude());
	sb.append('\n');

	sb.append("Longitude: ");
	sb.append(location.getLongitude());
	sb.append('\n');

	sb.append("Altitiude: ");
	sb.append(location.getAltitude());
	sb.append('\n');

	sb.append("Accuracy: ");
	sb.append(location.getAccuracy());
	sb.append('\n');

	sb.append("Timestamp: ");
	Date timestamp = new Date(location.getTime());
	sb.append(timestamp.toLocaleString());
	sb.append('\n');

	txtInfo.setText(sb.toString());
	   }
	   */

	private void fillData()
	{
		// Get all of the positions from the database
		Cursor positionsCursor = mDbHelper.fetchAllTracks();
		startManagingCursor(positionsCursor);

		Log.d(tag, " **************** " + positionsCursor.toString());
		Log.d(tag, " **************************************");

		//String[] from = new String[] { DbAdapter.KEY_TIMESTAMP };
		//String[] from = new String[] { GpsDbAdapter.KEY_LATITUDE.toString(), GpsDbAdapter.KEY_LONGITUDE.toString()  };
		//int[] to = new int[] { R.id.text1 };

		// Now create an array adapter and set it to display using our row
		ListAdapter positionsAdapter = new SimpleCursorAdapter(this,
				R.layout.track_row,
				positionsCursor,
				new String[] {DbAdapter.KEY_NAME},
				new int[] {R.id.row_title});

		setListAdapter(positionsAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);

		//mtrainoid.setTrack(id);

		// Maybe the map activity is not created yet
		//        if (mtrainoid.mMap != null)
		//            mtrainoid.mMap.mapView.invalidate();

		service.setFollowing(false); // stop following. we just want to show the track

		Intent i = new Intent(this, Map.class);
		i.putExtra("mTrackId", id);
		startActivityForResult(i, trainoid.ACTIVITY_MAP);
	}

	@Override
	protected void onStop()
	{
		/* may as well just finish since saving the state is not important for this toy app */
		finish();
		mDbHelper.close();
		super.onStop();
	}
}
